<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_groups extends CI_Model {

    private $day_types_count = array();

    function __construct() {
        parent::__construct();
    }

    public function delete_subject_day($subject) {
        $day_id = intval($this->input->post('delete_day_id'));
        $this->db->where('id', $day_id);
        $this->db->delete('subjects_days'); //deleting subject's current day
        $this->db->where('subject_day_id', $day_id); //deleting all visits for this day
        $this->db->delete('visits');
        return $this->db->_error_number();
    }

    public function edit_subject_day() {
        $day = intval($this->input->post('day_id'));
        $data['type'] = $this->input->post('edit_type');
        $data['note'] = trim($this->input->post('edit_note'));
        $this->db->where('id', $day);
        $this->db->update('subjects_days', $data);
        return $this->db->_error_number();
    }

    public function insert_subject_day($gst_id, $date, $type) {
        //$this->db->where('gst_id', $gst_id);
        $q = $this->db->insert('subjects_days', array('date' => $date, 'gst_id' => $gst_id, 'type' => $type));
        return $this->db->affected_rows();
    }

    public function get_subject_days($gst_id, $order1='date', $order2='asc') {
        $this->day_types_count = array($gst_id => array('l' => 0, 'p' => 0));
        $this->db->where('gst_id', $gst_id);
        $this->db->order_by($order1, $order2);
        $this->db->order_by('date', 'asc');
        $q = $this->db->get('subjects_days');
        foreach ($q->result_array() as $item) {
            $this->day_types_count[$gst_id][$item['type']]++;
            $result[$item['id']] = $item;
        }
        return $q->result_array();
    }

    public function get_subject_info($gst_id) {
        return $this->day_types_count[$gst_id];
    }

    /*
     * @returns: array[student_ud][subject_day_id]
     */

    public function get_visits($gst_id) {
        $this->db->where('gst_id', $gst_id);
        $q = $this->db->get('visits');
        $visits = array();
        foreach ($q->result_array() as $item) {
            $visits[$item['student_id']][$item['subject_day_id']] = $item;
        }
        return $visits;
    }

    public function get_groups() {
        $this->db->order_by('code', 'asc');
        $query = $this->db->get('groups', 20);
        return $query->result_array();
    }

    public function get_students($group_id) {
        $this->db->where('id_group', $group_id);
        $this->db->order_by('name', 'asc');
        $q = $this->db->get('students');
        return $q->result_array();
    }

    public function get_subjects($group_id) {
        $this->db->select('GST.*,
            G.code AS group_code,
            S.name AS subject_name,
            T.name as teacher_name');
        $this->db->from('group_subject_teacher as GST');
        $this->db->join('groups as G', 'GST.id_group=G.id', 'left');
        $this->db->join('subjects as S', 'GST.id_subject=S.id', 'left');
        $this->db->join('teachers as T', 'GST.id_teacher=T.id', 'left');
        $this->db->where('id_group', $group_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_one_subject($group, $subject) {
        $this->db->select('GST.*,
            G.code AS group_code,
            S.name AS subject_name,
            T.name as teacher_name');
        $this->db->from('group_subject_teacher as GST');
        $this->db->join('groups as G', 'GST.id_group=G.id', 'left');
        $this->db->join('subjects as S', 'GST.id_subject=S.id', 'left');
        $this->db->join('teachers as T', 'GST.id_teacher=T.id', 'left');
        $this->db->where('GST.id', $subject);
        $this->db->where('GST.id_group', $group);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_one_group($id = 0, $type='array') {
        $query = $this->db->query('SELECT * FROM groups WHERE id = "' . $id . '" ');
        switch ($type) {
            case 'array': return $query->result_array();
                break;
            case 'row': return $query->row_array();
                break;
        }
    }

    public function insert_group($code = '', $manager = '') {

        $query = $this->db->query('SELECT code FROM groups WHERE code = "' . $code . '" ');
        if ($query->num_rows() > 0) {
            return sprintf(lang('admin_group_required'), $code);
        } else {
            $this->db->insert('groups', array('code' => $code, 'manager' => $manager));
            return lang('successfull');
        }
    }

    public function edit_group($id = 0, $code = '', $manager = '') {
        $query = $this->db->query('SELECT id,code FROM groups WHERE code = "' . $code . '" ');
        if ($query->num_rows() > 0) {
            $row = $query->row();
            if ($row->id != $id) {
                return sprintf(lang('admin_group_required'), $code);
            } else {
                $this->db->update('groups', array(
                    'code' => $code,
                    'manager' => $manager), array('id' => $id));
                return lang('successfull');
            }
        } else {
            $this->db->update('groups', array(
                'code' => $code,
                'manager' => $manager), array('id' => $id));
            return lang('successfull');
        }
    }

    public function delete_group($groups = array()) {

        foreach ($groups as $id) {
            $this->db->delete('groups', array('id' => $id));
        }
    }

}

/* End of file m_teachers.php */
/* Location: ./application/models/m_teachers.php */